package org.ticlib.task
{
	import com.google.analytics.utils.UserAgent;
	
	import mx.rpc.AsyncToken;
	import mx.rpc.Responder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class DelegateInvoker extends InvokerBase
	{
		public function DelegateInvoker()
		{
			super();
			responder = new Responder(onResult, onFault);
		}

		[Bindable] public var operation:Function;
		
		private var responder:Responder;
		
		override public function stop():void
		{
			
		}
		
		override public function execute():void
		{
			initialize();
			
			if(isUseCache()) return;
			
			var token:AsyncToken;
			if(this.arguments == null)
				token = operation.call(this);
			else
				token = operation.apply(this, this.arguments);
			token.addResponder(responder);
		}
		
		private function onResult(e:ResultEvent):void
		{
			signalComplete(e.result);
		}
		
		private function onFault(e:FaultEvent):void
		{
			faultObject = e.fault;
			dispatchEvent(FaultEvent.createEvent(e.fault));
		}
	}
}